package com.ahdy.controller;

import com.ahdy.entity.ElderlyUser;
import com.ahdy.service.ElderlyUserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老人用户管理控制器
 */
@RestController
@RequestMapping("/api/elderly-users")
public class ElderlyUserController {

    @Autowired
    private ElderlyUserService elderlyUserService;

    /**
     * 添加老人用户
     *
     * @param elderlyUser 老人用户实体
     * @return 响应结果
     */
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> addElderlyUser(@Valid @RequestBody ElderlyUser elderlyUser) {
        Integer id = elderlyUserService.addElderlyUser(elderlyUser);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "添加老人用户成功");
        response.put("data", id);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新老人用户信息
     *
     * @param id          用户ID
     * @param elderlyUser 老人用户实体
     * @return 响应结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateElderlyUser(@PathVariable Integer id,
            @Valid @RequestBody ElderlyUser elderlyUser) {
        elderlyUser.setId(id);
        int rows = elderlyUserService.updateElderlyUser(elderlyUser);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "更新老人用户信息成功");
        } else {
            response.put("code", 404);
            response.put("message", "老人用户不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 删除老人用户
     *
     * @param id 用户ID
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteElderlyUser(@PathVariable Integer id) {
        int rows = elderlyUserService.deleteElderlyUser(id);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "删除老人用户成功");
        } else {
            response.put("code", 404);
            response.put("message", "老人用户不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID查询老人用户
     *
     * @param id 用户ID
     * @return 响应结果
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getElderlyUserById(@PathVariable Integer id) {
        ElderlyUser elderlyUser = elderlyUserService.getElderlyUserById(id);

        Map<String, Object> response = new HashMap<>();
        if (elderlyUser != null) {
            response.put("code", 200);
            response.put("message", "获取老人用户信息成功");
            response.put("data", elderlyUser);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "老人用户不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 查询所有老人用户
     *
     * @return 响应结果
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllElderlyUsers() {
        List<ElderlyUser> elderlyUsers = elderlyUserService.getAllElderlyUsers();

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取老人用户列表成功");
        response.put("data", elderlyUsers);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据手机号查询老人用户
     *
     * @param phone 手机号
     * @return 响应结果
     */
    @GetMapping("/by-phone/{phone}")
    public ResponseEntity<Map<String, Object>> getElderlyUserByPhone(@PathVariable String phone) {
        ElderlyUser elderlyUser = elderlyUserService.getElderlyUserByPhone(phone);

        Map<String, Object> response = new HashMap<>();
        if (elderlyUser != null) {
            response.put("code", 200);
            response.put("message", "获取老人用户信息成功");
            response.put("data", elderlyUser);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "老人用户不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 用户登录
     *
     * @param loginParams 登录参数
     * @return 响应结果
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> loginParams) {
        String phone = loginParams.get("phone");
        String password = loginParams.get("password");

        ElderlyUser elderlyUser = elderlyUserService.login(phone, password);

        Map<String, Object> response = new HashMap<>();
        if (elderlyUser != null) {
            // 更新最后登录时间
            elderlyUserService.updateLastLogin(elderlyUser.getId(), LocalDateTime.now());

            response.put("code", 200);
            response.put("message", "登录成功");
            response.put("data", elderlyUser);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 401);
            response.put("message", "手机号或密码错误");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
        }
    }

    /**
     * 根据状态查询老人用户
     *
     * @param status 用户状态
     * @return 响应结果
     */
    @GetMapping("/by-status/{status}")
    public ResponseEntity<Map<String, Object>> getElderlyUsersByStatus(@PathVariable ElderlyUser.UserStatus status) {
        List<ElderlyUser> elderlyUsers = elderlyUserService.getElderlyUsersByStatus(status);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取指定状态的老人用户列表成功");
        response.put("data", elderlyUsers);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据子女ID查询关联的老人列表
     *
     * @param familyId 子女用户ID
     * @return 响应结果
     */
    @GetMapping("/by-family/{familyId}")
    public ResponseEntity<Map<String, Object>> getElderlyUsersByFamilyId(@PathVariable Integer familyId) {
        List<ElderlyUser> elderlyUsers = elderlyUserService.getElderlyUsersByFamilyId(familyId);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取关联老人列表成功");
        response.put("data", elderlyUsers);
        return ResponseEntity.ok(response);
    }

    /**
     * 统计活跃用户数量
     *
     * @return 响应结果
     */
    @GetMapping("/statistics/active")
    public ResponseEntity<Map<String, Object>> countActiveUsers() {
        int count = elderlyUserService.countActiveUsers();

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取活跃用户统计成功");
        response.put("data", count);
        return ResponseEntity.ok(response);
    }
}
